$(document).ready(function() {

    init();
    get_time_diff();
    get_time_till_due();
    comments();
    tinymce.init({
        selector: 'textarea.textarea',
        // plugins: "image, spellchecker, link, table, code, colorpicker, hr",
        plugins: [
            "advlist autolink link image lists charmap print preview hr anchor pagebreak",
            "searchreplace wordcount visualblocks visualchars insertdatetime media nonbreaking",
            "table contextmenu directionality emoticons paste textcolor responsivefilemanager"
        ],
        menubar: false,
        toolbar1: "undo, redo, bold, italic, underline, link, code, alignleft, aligncenter, alignright,  bullist, numlist,  styleselect, image,  table, forecolor,backcolor ",
        theme: "modern",
        resize: false,
        spellchecker_wordchar_pattern: /[^\s,\.]+/g,
        skin: "light",
        height: 200,
        image_advtab: true,
        external_filemanager_path: "../filemanager/",
        filemanager_title: "Filemanager",
        external_plugins: {
            "filemanager": "/filemanager/plugin.min.js"
        }
    });




    function init() {

        $('[rel="popover"]').popover();
        $('[rel="tooltip"]').tooltip({
            'trigger': 'hover'
        });
        $('a[rel="popover"]').on('click', function(e) {
            e.preventDefault();
        })

        $('.validation').hover(
            function() {
                $(this).addClass('fadeout')
            }
        )

        $.each($('.table-roles'), function(i, el) {
            if ($(el).text() == '') {
                $(el).text("Owner");
            }
        })

        $('#update_task form :input').change(function() {
            $('#edit_update').attr('disabled', false);
        });


        $('#textarea_comment').on('focus keyup', function() {
            $('#textarea_submit').attr('disabled', false);
        });
        $('#textarea_comment').on('blur', function() {
            $('#textarea_comment').val() === '' ? $('#textarea_submit').attr('disabled', true) : $('#textarea_submit').attr('disabled', false);
        })

        $('form input:first-child').focus();

        $('p.valid').hover(
            function() {
                $(this).fadeOut('fast');
                $(this).prev('input').focus();
            }
        )

        $('input[name="date"], input[name="edit_date_due"]').datepicker({
            minDate: 0
        });

        $('.logout-btn').on('click', logout);

    }

    function comments() {
        if ($("#no-comments").is(":visible")) {
            $('#add_comment_form').hide();
            $("#show-textarea").on('click', function(e) {
                e.preventDefault();
                $("#no-comments").fadeOut('300');
                setTimeout(function() {
                    $('#add_comment_form').slideDown('slow');
                }, 500);
            });
        } else {
            $('#add_comment_form').show();
        }
    }

    function get_time_diff() {
        $.each($('.time_passed'), function(i, el) {
            var timestamp = $.timeago($(el).attr('data-timestamp'));
            $(el).text(timestamp);
        });
    }

    function get_time_till_due() {
        $.each($('.time_till_due'), function(i, el) {
            $.timeago.settings.allowFuture = true;
            var timestamp = $.timeago($(el).attr('data-timestamp'));
            $(el).text(timestamp);
        });
    }

    function logo_animation() {
        $('#logo').css({
            'visibility': 'visible'
        }).addClass('logo_animation');
        setTimeout(function() {
            $('#login_form').removeClass('hidden').addClass('fadein');
        }, 2000);
    }

    // function cancel(e) {

    // }

    function logout(e) {

        e.preventDefault();
        $.ajax({
            url: window.location.origin + '/user/logout',
            method: 'GET',
            success: function(data) {
                window.location.replace(window.location.origin + "/logout_success");
            }
        });

    }


})
